package com.solidtrust.crawler.framework;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;

import com.solidtrust.crawler.model.PageSource;
import com.solidtrust.entity.CarCategory;
import com.solidtrust.entity.IScrapeEntity;
import com.solidtrust.repository.CarCategoryRepository;

public abstract class DistributedSitemap extends BaseSitemap{

	private static final Logger log4j = Logger.getLogger(DistributedSitemap.class);
	
	@Autowired CarCategoryRepository cateRep;
	private String urlPrefix = null;
	
	@Override
	public void start() throws Exception {
		if(running == true){
			log4j.info(getSource() + " is running");
			return;
		}
		running = true;
		
		startDate = DateUtils.truncate(new Date(), Calendar.DATE);
		log4j.info("Start to crawl " + getSource() + " on " + startDate);
		log4j.info("[" + getSource() + "] start from last ID: " + commonDao.lastId(getEntityClass()));
//		log4j.info("[" + getSource() + "]" + commonDao.countUnsoldCarIds(getEntityClass()) + " unsold cars");
//		log4j.info("[" + getSource() + "]" + commonDao.countSoldCarIds(getEntityClass()) + " sold cars");
		
		while(true){
			startDate = DateUtils.truncate(new Date(), Calendar.DATE);
			CarCategory cate = commonDao.findOneByUnlock(getSource());
			if(cate == null){
				break;
			}
			cate.setLocking(true);
			cateRep.save(cate);
			
			urlPrefix = cate.getUrl().substring(0, cate.getUrl().indexOf("."));
			
			log4j.info("Load car category: " + cate);
			unsold = commonDao.getUnsoldCarIds(getEntityClass(), cate);
			log4j.info("[" + getSource() + "]" + unsold.size() + " unsold cars");
			
			sold = commonDao.getSoldCarIds(getEntityClass(), cate);
			log4j.info("[" + getSource() + "]" + sold.size() + " sold cars");
			
			nextPage(cate.toPageSource(), 1, cate.getModel(), cate.getBrand(), cate.getCity());
			
			//save rest cars;
			persist();
			log4j.info("[" + getSource() + "]" + "Scrape " + news.size() + " cars");
			//touch old records
			log4j.info("[" + getSource() + "]" + "To touch " + existing.size() + " records");
			touchOrRemove();
			log4j.info("Total requests: " + totalRequests);
		}
		
		finish();
		log4j.info("Total requests: " + totalRequests);
		running = false;
	}
	
	protected boolean isSameCity(String url){
		return url.startsWith(urlPrefix);
	}
	
//	protected boolean checkExist(String carId, boolean markSold){
//		IScrapeEntity car = commonDao.findBySourceId(carId, getEntityClass());
//		if(car != null){
//			if(car.getRemovedDate() != null){
//				log4j.info(carId + " has been sold last time");
//				return false;
//			}
//			
//			log4j.info(carId + " exists in DB");
//			if(markSold == true){
//				log4j.info("but marked sold");
//				car.setRemovedDate(startDate);
//				commonDao.save(car);
//			}else{
//				car.setTouchDate(startDate);
//				commonDao.save(car);
//			}
//			return false;
//		}
//		
//		return true;
//	}
	
	protected void toPersist(IScrapeEntity entity){
		entity.setScrapedDate(startDate);
		commonDao.save(entity);
	}
	
	@Override
	public String getStartUrl() {
		return null;
	}
	
	@Override
	public List<PageSource> getBrands(Document doc, PageSource page) {
		return null;
	}
	
	@Override
	public List<PageSource> getModels(Document doc, PageSource page) {
		return null;
	}
	
	@Override
	public List<PageSource> getCitiesOrProvinces(Document doc) {
		return null;
	}
	
	@Override
	public void finish() {
		long count = cateRep.countBySourceAndLocking(getSource(), false);
		if(count == 0){
			int update = commonDao.unlockCategories(getSource());
			log4j.info("Unlock categories: " + update);
		}
	}
}
